package com.leyou.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.dto.BrandDTO;
import com.leyou.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandServcie;
    /*
    * 分页查询品牌
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
    * */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy,
            @RequestParam(value = "desc", defaultValue = "true") Boolean desc
    ){
        return ResponseEntity.ok(brandServcie.queryBrandByPage(key,page,rows,sortBy,desc));
    }
    /*
    新增品牌
    */

    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brand){
         brandServcie.saveBrand(brand,brand.getCids());
         return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /*
    * 根据品牌查询分类
    *
    * */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategroyId(@RequestParam("id") Long cid){
        return ResponseEntity.ok(this.brandServcie.queryBrandByCategroyId(cid));

    }
    /*
    根据分类id查询品牌
    */

    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id){
        BrandDTO brand=brandServcie.queryById(id);
        return ResponseEntity.ok(brand);
    }

    /*
    * 根据品牌id集合查询品牌
    * @param idList 品牌id的集合
    * @return 品牌的集合
    * */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList){
        return ResponseEntity.ok(brandServcie.queryByIds(idList));
    }

}
